package org.nowcoder.leetcode.backTracking;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Title  : 46. Permutations
 * Source : https://leetcode.cn/problems/permutations/description/
 * Author : XrazYang
 * Date   : 2024-1-25
 */

public class LeetCode_46 {
    private final List<List<Integer>> result = new ArrayList<>();
    private final LinkedList<Integer> path = new LinkedList<>();

    public List<List<Integer>> permute(int[] nums) {
        permuteHelper(nums);
        return result;
    }

    private void permuteHelper(int[] nums) {
        if (path.size() == nums.length) {
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (path.contains(nums[i])) continue;
            path.add(nums[i]);
            permuteHelper(nums);
            path.removeLast(); //回溯，撤销处理的节点
        }
    }
}
